<template>
    <div style="padding: 0 17px">
        <Breadcrumb/>
        <div style="display: flex;margin-top: 20px">
            <!--      <el-button icon="Delete" type="danger" style="margin-right: 15px" @click="deleteSelect">删除所选-->
            <!--      </el-button>-->
            <span style="min-width: 200px;margin-right: 15px">
          <OrganizationTree
                  @change="load"
                  v-model="selectedOrganizationId"
                  @tree-select-changed="treeSelectChanged"
          />
      </span>
            <div style="width: 350px;display: flex">
            <el-date-picker
                    style="margin-right: 15px"
                    v-model="searchData"
                    type="daterange"
                    range-separator="到"
                    start-placeholder="开始时间"
                    end-placeholder="结束时间"
                    value-format="YYYY-MM-DD"
            />
            </div>
            <el-input v-model="search" placeholder="单位名称/通知人/公告内容"
                      style="width: 200px;height: 33px;margin-right: 15px;font-size: 13px;" clearable></el-input>
            <el-button icon="Search" type="primary" style="color: #fff;font-size: 12px;background-color: #1890ff;"
                       @click="load">搜索
            </el-button>
            <el-button type="success" style="" @click="importData">导入</el-button>
            <el-button type="success" style="" @click="exportData">导出</el-button>
        </div>
        <div>
            <el-table :data="tableData" :row-style="{ height: '77px' }" border
                      style="width: calc(100vw - 240px);margin-top: 20px;height: 70vh;color: #909399;font-size: 15px;"
                      @selection-change="handleSelectionChange">
                <!--<el-table-column-->
                        <!--type="selection"-->
                        <!--width="55">-->
                <!--</el-table-column>-->
                <el-table-column prop="id" label="ID" width="100"/>
                <el-table-column prop="clientName" label="单位名称" width="200"/>
                <el-table-column prop="username" label="通知人" width="150"/>
                <el-table-column prop="content" label="公告内容" width="auto"/>
                <el-table-column prop="createTime" label="公告时间" width="180"/>
                <el-table-column prop="state" label="状态" align="center" width="100">
                    <template v-slot="scope">
                        <el-tag type="success" v-if="scope.row.state === '0'">未读</el-tag>
                        <el-tag type="danger" v-else-if="scope.row.state === '1'">已读</el-tag>
                    </template>
                </el-table-column>
                <!--        <el-table-column align="center" label="操作" fixed="right" width="100">-->
                <!--          <template v-slot="scope">-->
                <!--            <el-button type="danger" size="small" icon="Delete" @click="deleteItem(scope.row.id)">删除</el-button>-->
                <!--          </template>-->
                <!--        </el-table-column>-->
            </el-table>
            <el-pagination
                    :current-page="currentPage"
                    :page-size="pageSize"
                    :page-sizes="[10, 20, 30, 50]"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total"
                    :background="true"
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
            />
        </div>
    </div>
    <el-dialog title="导入" v-model="dialogImportVisible" width="520px">
        <el-form label-position="right" label-width="100px">
            <el-form-item label="模板下载:" padding-top:10px>
                <el-button type="primary" size="small" style="" @click="exportTemplate">通知记录</el-button>
            </el-form-item>
            <el-form-item label="文件" padding-top:-20px>
                <el-upload :headers="headers"
                           :multiple="false"
                           :on-success="onUploadSuccess"
                           :on-error="onUploadError"
                           :action="serverIp+'/announce/importData'"
                           class="upload-demo">
                    <el-button size="small" type="primary">点击上传</el-button>
                    <div slot="tip" class="el-upload__tip">只能上传xlsx文件</div>
                </el-upload>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogImportVisible = false">取消</el-button>
        </div>
    </el-dialog>
</template>

<script>
    import request from "@/utils/request.js";
    import Breadcrumb from "@/components/Breadcrumb.vue";
    import OrganizationTree from "@/components/OrganizationTree.vue";
    import {serverIp} from "../../../public/static/config"

    export default {
        name: "RecordNotice",
        components: {Breadcrumb, OrganizationTree},
        data() {
            return {
                serverIp,
                headers: {
                    token:  JSON.parse(localStorage.getItem('user')).token,
                },
                dialogImportVisible: false,
                tableData: [],
                searchData: [],
                selectedOrganizationId: JSON.parse(localStorage.getItem("user")).organization,
                search: "",
                currentPage: 1,
                pageSize: 10,
                total: 10,
                // 选择框
                multipleSelection: []
            }
        },
        created() {
            this.load()
        },
        methods: {
            importData() {
                this.dialogImportVisible = true;
            },
            onUploadSuccess() {
                this.$message.success('上传成功');
                this.dialogImportVisible = false;
                location.reload();
            },
            onUploadError() {
                this.$message.error('上传失败');
            },
            // 模板下载
            exportTemplate() {
                window.location.href = '../xlsx/announce.xlsx'
            },
            exportData() {
                let level = JSON.parse(localStorage.getItem("user")).level
                let isAllClient = JSON.parse(localStorage.getItem("user")).isAllClient;
                let clientIds;
                if (isAllClient === "1") {
                    clientIds = Object.values(JSON.parse(localStorage.getItem("user")).clientIds);
                } else {
                    clientIds = [];
                }
                let startTime = "", endTime = ""
                if (this.searchData !== null && this.searchData.length !== 0) {
                    startTime = this.searchData[0]
                    endTime = this.searchData[1]
                }
                window.open(serverIp + '/announce/export?level=' + level + "&organizationId=" + this.selectedOrganizationId + "&isAllClient=" + isAllClient + "&clientIds=" + clientIds + "&searchParam=" + this.search + "&startTime=" + startTime + "&endTime=" + endTime);
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
                console.log(this.multipleSelection)
            },
            handleSizeChange(pageSize) {   // 改变当前每页的个数触发
                this.pageSize = pageSize
                this.load()
            },
            handleCurrentChange(pageNum) {  // 改变当前页码触发
                this.currentPage = pageNum
                this.load()
            },
            treeSelectChanged(data) {
                this.currentData = data
            },
            /*
            // 删除单个
            deleteItem(scope){
              this.$confirm('是否确定删除?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
              }).then(() => {
                request.get("/record-notice/delete/"+scope).then(res=>{
                  if (res.code==='200'){
                    this.$message.success("成功")
                    this.load()
                  }else {
                    this.$message.error("删除失败，"+res.msg)
                  }
                })
              });
            },
            deleteSelect(){
              this.$confirm('是否确定删除?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
              }).then(() => {
                if (this.multipleSelection.length===0){
                  this.$message.error("未选择数据")
                  return
                }
                const idList=[];
                this.multipleSelection.forEach(row => {
                  idList.push(row.id)
                });
                request.post("/record-notice/deleteByIdList",idList).then(res=>{
                  if (res.code==='200'){
                    if (res.data){
                      this.$message.success("成功")
                    }else {
                      this.$message.error("删除失败")
                    }
                    this.load()
                  }else {
                    this.$message.error("删除失败，"+res.msg)
                  }
                })
              });
            },

             */
            // 搜索及初始化
            load() {
                let level = JSON.parse(localStorage.getItem("user")).level
                let isAllClient = JSON.parse(localStorage.getItem("user")).isAllClient;
                let clientIds;
                if (isAllClient === "1") {
                    clientIds = Object.values(JSON.parse(localStorage.getItem("user")).clientIds);
                } else {
                    clientIds = [];
                }
                let startTime = "", endTime = ""
                if (this.searchData !== null && this.searchData.length !== 0) {
                    startTime = this.searchData[0]
                    endTime = this.searchData[1]
                }
                request.get("/announce/page?pageNum=" + this.currentPage + "&pageSize=" + this.pageSize + "&level=" + level + "&organizationId=" + this.selectedOrganizationId + "&isAllClient=" + isAllClient + "&clientIds=" + clientIds + "&searchParam=" + this.search + "&startTime=" + startTime + "&endTime=" + endTime).then(res => {
                    if (res.code === '200') {
                        for (let key in res.data) {
                            if (key === "record") {
                                this.tableData = res.data[key];
                            } else if (key === "total") {
                                this.total = res.data[key];
                            }
                        }
                    } else {
                        this.$message.error("错误" + res.msg)
                    }
                })
            }
        }

    }
</script>

<style scoped>
    :deep(.el-table th .cell) {
        font-weight: 700
    }

    :deep(.el-form-item__label) {
        font-size: 14px;
        font-weight: 700
    }

    .input-size {
        font-size: 13px;
    }
</style>
